//
//  PokeMasterModel.swift
//  ToDo
//
//  Created by yanhangpu on 2021/7/13.
//

import Foundation

struct PokeMasterRoleInfo: Codable, Hashable {
    let imageName: String
    let nickName: String
    let englishName: String
    let height: String
    let weight: String
}

struct PokeMasterItem: Codable, Hashable {
    let roleId: String
    let color: String
    let expanded: Int
    let roleInfo: PokeMasterRoleInfo
}

struct PokeMasterModel: Codable {
    let pokeMasterItem: [PokeMasterItem]
}

extension PokeMasterModel {

    static func loadPokeMasterData<T: Decodable>() -> T {
        return load(fileName: "PokeMaster.json")
    }
    
    static func load<T: Decodable>(fileName: String) -> T {
        let data: Data
        guard let fileUrl = Bundle.main.url(forResource: fileName, withExtension: nil) else {
            fatalError("file \(fileName) error")
        }
        
        do {
            data = try Data(contentsOf: fileUrl)
        } catch _ as Error? {
            fatalError("file \(fileName) read error")
        }
        
        do {
            return  try JSONDecoder().decode(T.self, from: data)
        } catch  {
            fatalError("parse \(fileName) read error")
        }
    }
}
